﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Systematize.Domain.Data;
using Systematize.Domain.Entities;

namespace Systematize.Domain.Services
{
    public class LibraryItemServiceAsync<T> : LibraryItemService<T>, ILibraryItemServiceAsync<T>
        where T : EditableEntity
    {
        private readonly IRepositoryAsync<T> _repository;

        public LibraryItemServiceAsync(IRepositoryAsync<T> repository)
            : base(repository)
        {
            _repository = repository;
        }

        public async Task UpdateAsync(T entity)
        {
            entity.ModifiedOn = DateTime.Now;
            await _repository.UpdateAsync(entity);
        }

        public async Task CreateAsync(T entity)
        {
            await _repository.InsertAsync(entity);
        }

        public async Task<int> CountAsync()
        {
            return await _repository.CountAsync();
        }

        public async Task<int> CountAsync(Expression<Func<T, bool>> predicate)
        {
            return await _repository.CountAsync(predicate);
        }

        public async Task<T> GetByIdAsync(int id)
        {
            return await _repository.FindAsync(i => i.Id == id);
        }

        public async Task<IEnumerable<T>> GetAllAsync()
        {
            return await _repository.GetAllAsync();
        }

        public async Task<T> FindAsync(Expression<Func<T, bool>> predicate)
        {
            return await _repository.FindAsync(predicate);
        }

        public async Task<IEnumerable<T>> FindAllAsync(Expression<Func<T, bool>> predicate)
        {
            return await _repository.FindAllAsync(predicate);
        }
    }
}
